package edu.biwu.cookie;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * 获取cookie的方法
 *  利用request中的方法
 *      Cookie[] getCookies():获取浏览器发送过来的所有cookie对象,如果浏览器没有发送任何cookie,该方法返回null
 *  取出Cookie的name和value,可以利用Cookie中的方法
 *      public String getName():取出cookie中封装的name
 *      public String getValue():取出cookie中封装的value
 *
 */
@WebServlet("/cookie/receive")
public class CookieReceive extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
